package controllers;

import play.Play;
import play.Logger;
import play.mvc.*;
import models.*;

import java.util.*;

import net.solosky.litefetion.*;
import net.solosky.litefetion.bean.*;
import net.solosky.litefetion.notify.*;

public class Fetions extends Application {

    public static void loginVerifyImg(String mobile) {
        Fetion fetion  = Fetion.findOrNewByMobile(mobile);
        fetion.resetLoginVerifyImg();
        renderArgs.put("fetion",fetion); 
        renderTemplate("/Fetions/loginVerifyImg.xml");
    }
    
    public static void login(String mobile, String password, String verifyCode) {
        debugParams();
        Fetion fetion  = Fetion.findOrNewByMobile(mobile);
        ActionResult result = fetion.login(password,verifyCode);
        Logger.debug("登录结果：" + result);
        Logger.debug("状态：" + fetion.getClientState());
        renderArgs.put("result",result); 
        renderArgs.put("fetion",fetion); 
        renderTemplate("/Fetions/login.xml");
    }
    
    public static void notifies(String mobile) {
      Fetion fetion  = Fetion.findOrNewByMobile(mobile);
      List<Notify> notifies = fetion.getNotifies();
      List<ApplicationConfirmedNotify> applicationConfirmedNotifies = new Vector<ApplicationConfirmedNotify>();
      List<BuddyApplicationNotify> buddyApplicationNotifies = new Vector<BuddyApplicationNotify>();
      List<BuddyMessageNotify> buddyMessageNotifies = new Vector<BuddyMessageNotify>();
      List<BuddyStateNotify> buddyStateNotifies = new Vector<BuddyStateNotify>();
      List<ClientStateNotify> clientStatNotifies = new Vector<ClientStateNotify>();
      for(Notify notify : notifies) {
        if(notify.getType().equals(NotifyType.APPLICATION_CONFIRMED)) {
          applicationConfirmedNotifies.add((ApplicationConfirmedNotify)notify);
        } else if(notify.getType().equals(NotifyType.BUDDY_APPLICATION)) {
          buddyApplicationNotifies.add((BuddyApplicationNotify)notify);
        } else if(notify.getType().equals(NotifyType.BUDDY_MESSAGE)) {
          buddyMessageNotifies.add((BuddyMessageNotify)notify);
        } else if(notify.getType().equals(NotifyType.BUDDY_STATE)) {
          buddyStateNotifies.add((BuddyStateNotify)notify);
        } else if(notify.getType().equals(NotifyType.CLIENT_STATE)) {
          clientStatNotifies.add((ClientStateNotify)notify);
        } 
      }
      Logger.debug("");
      Logger.debug("");
      Logger.debug("1. notifies.size :" + notifies.size());
      Logger.debug("2. buddyList.size : " + fetion.getBuddyList().size());
/*      for( Buddy buddy : fetion.getBuddyList()) {
        Logger.debug("===============================");
        Logger.debug("mobile : " + buddy.getMobile());
        Logger.debug("nick_name: " + buddy.getNickName());
        Logger.debug("user_id : " + buddy.getUserId());
        Logger.debug("s_id : " + buddy.getSid());
        Logger.debug("uri : " + buddy.getUri());
        Logger.debug("-------------------------------");
      }*/
      renderArgs.put("applicationConfirmedNotifies",applicationConfirmedNotifies); 
      renderArgs.put("buddyApplicationNotifies",buddyApplicationNotifies);
      renderArgs.put("buddyMessageNotifies",buddyMessageNotifies);
      renderArgs.put("buddyStateNotifies",buddyStateNotifies);
      renderArgs.put("clientStatNotifies",clientStatNotifies);
      renderArgs.put("fetion",fetion); 
         renderTemplate("/Fetions/notifies.xml");
    }
    
    public static void logout(String mobile) {
      debugParams();
      Fetion fetion  = Fetion.findOrNewByMobile(mobile);
      ActionResult result = fetion.logout();
      Logger.debug("result: " + result);
      Logger.debug("state: " + fetion.getClientState());
      renderArgs.put("result",result); 
      renderArgs.put("fetion",fetion); 
         renderTemplate("/Fetions/logout.xml");
    }
    
    public static void addBuddyVerifyImg(String mobile) {
        debugParams();
        Fetion fetion  = Fetion.findOrNewByMobile(mobile);
        fetion.resetAddBuddyVerifyImg();
        renderArgs.put("fetion",fetion); 
        renderTemplate("/Fetions/addBuddyVerifyImg.xml");
    }
    
    public static void addBuddy(String mobile, String distMobile, String localName, String verifyCode) {
        debugParams();
        Fetion fetion  = Fetion.findOrNewByMobile(mobile);
        ActionResult result = fetion.addBuddy(distMobile,localName,verifyCode);
        Buddy buddy = fetion.getBuddyByMobile(distMobile);
        Logger.debug("buddy : " + buddy);
        renderArgs.put("result",result); 
        renderArgs.put("fetion",fetion); 
        renderArgs.put("buddy",buddy);       
        renderTemplate("/Fetions/addBuddy.xml");
    }
    
    public static void sendMessages(String mobile, String distMobile, String fetionUserId, String content) {
        debugParams();
        Fetion fetion  = Fetion.findOrNewByMobile(mobile);
        ActionResult result = fetion.sendMessage(distMobile,fetionUserId,content);
        Logger.debug("result : " + result);
        renderArgs.put("result",result); 
        renderArgs.put("fetion",fetion); 
        renderTemplate("/Fetions/sendMessages.xml");
    }

    public static void clientState(String mobile) {
      debugParams();
      Fetion fetion  = Fetion.findOrNewByMobile(mobile);
      Logger.debug(mobile + "状态为" + fetion.getClientState());
      renderArgs.put("fetion",fetion); 
      renderTemplate("/Fetions/clientState.xml");  
    }

    private static void debugParams() {
        Logger.debug("1. 看看都传了啥参数");
        Iterator keySet = params.all().keySet().iterator();
        while(keySet.hasNext())  {
            String key = (String)keySet.next();
            Logger.debug(key + " : " + (String)params.get(key));
        }
    }
}